package com.huanxun.foodspath3;

import java.util.HashMap;
import java.util.Map;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.KeyEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import com.huanxun.application.MApplication;
import com.huanxun.bean.LoginBean;
import com.huanxun.engine.LoginEngine;
import com.huanxun.utils.Constants;
import com.huanxun.utils.MLogger;
import com.huanxun.utils.SystemUtils;

/**
 * 登录Activity
 *
 */
public class MainActivity extends Activity {

	private long exitTime = 0;
	
	
	private EditText et_email;
	private EditText et_pwd;
	private String email;
	private String pwd;

	private SharedPreferences sp;

	private Button bt_login;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		((MApplication) getApplication()).activities.add(this);
		sp = getSharedPreferences("config", Context.MODE_PRIVATE);
		boolean autoLogin = sp.getBoolean("autoLogin", false);
		String roleNumber = sp.getString("roleNumber", "");
		String loginname = sp.getString("loginname", "");
		String id = sp.getString("id", "");
		if(autoLogin){
			Intent intent = new Intent();
			intent.setClass(MainActivity.this, MessageActivity.class);
			intent.putExtra("roleNumber", roleNumber);
			intent.putExtra("loginname", loginname);
			intent.putExtra("id", id);
			startActivity(intent);
		}	
		initView();
		
	}

	private void initView() {
		bt_login = (Button) findViewById(R.id.bt_login);
		et_email = (EditText) findViewById(R.id.et_email);
		et_pwd = (EditText) findViewById(R.id.et_pwd);

		bt_login.setOnClickListener(new OnclickListener());

	}

	private class OnclickListener implements OnClickListener {

		

		@Override
		public void onClick(View v) {
			email = et_email.getText().toString().trim();
			pwd = et_pwd.getText().toString().trim();
			if(TextUtils.isEmpty(email)||TextUtils.isEmpty(pwd)){
				SystemUtils.toast(getApplicationContext(), "用户名或密码为空");
			}else{
				final Map<String,String> map = new HashMap<String, String>();
				map.put("loginName", email);
				map.put("password", pwd);
				//联网登录
				new Thread(){
					public void run() {
						String path = Constants.URL_LOGIN;
						try {
							String json = LoginEngine.loginUseHttpClientByPost(path, map);
							LoginBean fromJson = SystemUtils.gson.fromJson(json, LoginBean.class);
							MLogger.i("UserInfo", json);
							if(fromJson.code.equals("200")){
								String roleNumber = fromJson.data.userRole.id;
								String loginname =fromJson.data.loginName;
								String id =fromJson.data.id;
								sp.edit().putBoolean("autoLogin", true).commit();
								sp.edit().putString("roleNumber", roleNumber).commit();
								sp.edit().putString("loginname", loginname).commit();
								sp.edit().putString("id", id).commit();
								Intent intent = new Intent(MainActivity.this, MessageActivity.class);
								intent.putExtra("roleNumber", roleNumber);
								intent.putExtra("loginname", loginname);
								intent.putExtra("id", id);
								startActivity(intent);
								finish();
							}else{
								alertUser("用户名或密码错误");
							}
						} catch (Exception e) {
							e.printStackTrace();
							alertUser("网络错误");
						}
					};
				}.start();
			}
		}

	}

	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {
		if (keyCode == KeyEvent.KEYCODE_BACK
				&& event.getAction() == KeyEvent.ACTION_DOWN) {
			if ((System.currentTimeMillis() - exitTime) > 2000) {
				Toast.makeText(getApplicationContext(), "再按一次退出程序",
						Toast.LENGTH_SHORT).show();
				exitTime = System.currentTimeMillis();
			} else {
				finish();
				System.exit(0);
			}
			return true;
		}
		return super.onKeyDown(keyCode, event);
	}
	
	private void alertUser(final String text){
		runOnUiThread(new Runnable() {
			
			@Override
			public void run() {
				Toast.makeText(getApplicationContext(), text, 0).show();
			}
		});
	}
}
